/**
 * 
 */
package org.cuatrovientos.listofbrought;

import java.util.Vector;

/**
 * list with products
 * @author David
 *
 */
public class List extends Vector<Product> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int total_product=0;
	private float total_cost;
	
	
	public List(){
		
	}
	
	
	public int getTotal_product() {
		return total_product;
	}


	public void setTotal_product(int total_product) {
		this.total_product = total_product;
	}


	public float getTotal_cost() {
		return total_cost;
	}


	public void setTotal_cost(float total_cost) {
		this.total_cost = total_cost;
	}


	/**
	 * Insert product
	 * @param name
	 * @param quantity
	 * @param cost
	 * @return
	 */
	public String insertProducts(String name, int quantity, float cost){
		Product myproduct= new Product(name,quantity, cost);
		if (!checkProducts(myproduct)|| getTotal_product()<=100){
			this.add(myproduct);
			this.setTotal_cost(getTotal_cost()+myproduct.getCost());
			this.setTotal_product(this.size());
			return "Product insert correct";
		}
		return "Product insert incorrect";	
	}
	
	/**
	 * Delete Product 
	 * @param name
	 * @return
	 */
	public String removeProducts(String name){
		Product myproduct = new Product(name,0,0);
		int num=checkProducts(myproduct,10);
		if (num > -1){
			this.remove(num);
			return "Product remove correct";
		}
		return "Product remove incorrect";	
	}
	
	/**
	 * Show all product in list
	 */
	public void showAllProduct(){
		
		for(Product p: this){
			System.out.println(p.toString());
		}
		System.out.println(this.getTotal_cost());
	}
	
	private boolean checkProducts(Product myproduct){
		for (Product p : this){
			if (p.getName()==myproduct.getName()){
				return true;
			}
		}
		return false;
	}
	private int checkProducts(Product myproduct, int num){
		for (int i=0;i < this.size();i++){
			if(this.elementAt(i).getName().contains(myproduct.getName())){
				return i;
			}
		}
		return -1;
	}
	
}
	

